package com.DeliveryProgram.www.util;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

public class JDBCutil {

    private static String url;
    private static String user;
    private static String password;
    private static String driver;

    //读取资源文件并赋值
    static{
            try {
                Properties pro=new Properties();
                pro.load(new FileReader("src/jdbc.properties"));
                url= pro.getProperty("url");
                user= pro.getProperty("user");
                password= pro.getProperty("password");
                driver= pro.getProperty("driver");
                Class.forName(driver);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

    }

    //获取链接
    public static Connection getConnection() throws SQLException {

        return DriverManager.getConnection(url,user,password);
    }


    //释放资源
    public static void close(Statement stmt,Connection conn){
        if(stmt!=null){
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(conn!=null)
        {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }

    public static void close(ResultSet rs,Statement stmt,Connection conn){
        if(rs!=null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(stmt!=null){
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(conn!=null)
        {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }

}



